﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

namespace WebAssistant.Common
{
    public class ClassHelper
    {
        /// <summary>
        /// 调用并执行指定类里面的函数
        /// </summary>
        /// <param name="className">需要调用的类名(包含其命名空间)</param>
        /// <param name="methodName">需要调用的方法名</param>
        /// <param name="parameters">传递的参数值</param>
        public void GetAndExecuteMethod(string className, string methodName, object[] parameters = null)
        {
            try
            {
                var type = Type.GetType(className);
                if (type == null)
                    throw new NullReferenceException("类" + className + "不存在");

                var obj = type.Assembly.CreateInstance(className);
                //调用其方法
                var method = type.GetMethod(methodName);
                if (method == null)
                    throw new NullReferenceException("方法" + methodName + "不存在");

                //执行方法
                method.Invoke(obj, parameters);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 通过类名获取实例
        /// </summary>
        /// <param name="className"></param>
        /// <returns></returns>
        public static Type GetClassInstanceByClassName(string className)
        {
            Assembly[] assemblys = AppDomain.CurrentDomain.GetAssemblies();
            Type[] types = null;
            Type type = null;
            foreach (Assembly assembly in assemblys)
            {
                //得到此程序集中所有类型
                types = assembly.GetTypes();
                if (types != null)
                {
                    foreach (Type test in types)
                    {
                        //判断类型的名字是否与类名一致，忽视大小写
                        if (test.Name.Equals(className, StringComparison.CurrentCultureIgnoreCase))
                        {
                            type = test;
                            break;
                        }
                    }
                }
                if (type != null)
                {
                    break;
                }
            }
            return type;
        }

        public static Type GetClassByClassFullName(string classFullName)
        {
            Assembly assembly = Assembly.Load("OdooRpcClient"); //程序集名称
            //从程序集中获取指定对象类型;
            Type type = assembly.GetType(classFullName); //程序集名称.类名
            return type;
        }

        /// <summary>
        /// 通过实例获取类的全部属性信息(正序)
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static PropertyInfo[] GetClassPropertyInfoOrderByName(Type type)
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();
   ;
            PropertyInfo[] propertyInfos = type.GetProperties();            
            foreach (var propertyInfo in propertyInfos)
            {
                PropertyDescriptor propertyDescriptor = TypeDescriptor.GetProperties(type)[propertyInfo.Name];
                DescriptionAttribute descriptionAttribute = propertyDescriptor == null ? null : propertyDescriptor.Attributes[typeof(DescriptionAttribute)] as DescriptionAttribute;


            }


            return propertyInfos;
        }
    }
}
